package Controller;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.*;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Model.Contact;
import Model.ContactList;
import View.AddressBookView;
import View.DetailsContactView;
import View.ModifieContactView;
import View.NewContactView;

/**
 * The controller is responsible of the interactions between the view and the model
 * It holds all the main action calls of the application and all the listeners for the main JFrame
 */

public class AddressBookController implements ActionListener, ListSelectionListener, WindowListener,MouseListener,DocumentListener{
	
	private ContactList contactList;
	DefaultListModel<String> listModel;
	private Contact contact;
	private AddressBookController that = this; //Lookaround to allow listners to look at the class
	private AddressBookView view;
	
	public static String CREATE = "Create";
	public static String MODIFIE = "Modifie";
	public static String DELETE ="Delete";
	public static String DETAILS = "Details";
	public static String SAVE = "Save";
	public static String LOAD ="Load";
	public static String SEARCH ="Search";
	public static String SORTBYNAME = "Sort by Name";
	public static String SORTBYGROUP = "Sort by Group";
	public static String SORTBYDATE = "Sort by Date";
	public static String NOTHING_IN_THE_LIST = "Nothing in the list";
	public static String SAVE_QUESTION = "Would you like to save ?";
	
	public AddressBookController(AddressBookView view) {
		contactList = new ContactList();
		listModel = new DefaultListModel<String>();
		this.view=view;
	}
	
	public ContactList getContactList () {
		return contactList;
	}

	public void addContactAction(String firstName, String lastName, String group,
			String homeAddress, String compagnyName, String compagnyAddress,
			String persPhone, String proPhone, String webSite, String email,
			String birthDate, String imgURL, Date date) {
		
		Contact contact = new Contact(firstName, lastName, group,
				homeAddress, compagnyName, compagnyAddress,
				persPhone, proPhone, webSite, email,
				birthDate, imgURL, date);
		
		contactList.addContact(contact);
	}
	
	public void deleteContactAction(Contact contact) {
		contactList.deleteContact(contact);
	}
	
	public void updateContactAction(Contact contact) {
		
	}
	
	public DefaultListModel<String> getListModelAction() {
		listModel = contactList.getListModel();
		
		return listModel;
	}
	public DefaultListModel<String> getGroupsListAction() {
		listModel = contactList.getGoupsListModel();
		
		return listModel;
	}
	public DefaultListModel<String> getListModelByGroupAction(String group) {
		listModel = contactList.getListModelByGroup(group);
		
		return listModel;
	}
	public DefaultListModel<String> getListModelSearchAction(String name) {
		listModel = contactList.getListModelSearch(name);
		
		return listModel;
	} 
	public Contact getContact () {
		return contact;
	}
	public void setContact(Contact contact) {
		this.contact=contact;
	}
	
	public void findContactAction(String firstNameLastName) {
		String names[]=firstNameLastName.split(" ");
		contact=contactList.findContract(names[0], names[1]);
		
		
	}
	public boolean isContactExist(String FirstName, String LastName){
		if(contactList.findContract(FirstName, LastName)!=null){
			return true;
		}
		else{
			return false;			
		}

	}
	
	public void sortByNameAction(){
		contactList.sortByName();
	}
	public void sortByGroupAction(){
		contactList.sortByGroup();
	}
	public void sortByDateAction(){
		contactList.sortByDate();
	}
	public void saveAction(){
		contactList.saveContacts(contactList);
	}
	public void loadAction(){
		this.contactList=contactList.loadContacts();
	}
	public String getWebSiteAction(){
		return contact.getWebSite();
	}
	public String getContactAction(){
		return contact.getEmail();
	}
	
	
	
	////////////////////Listners///////////////////
	
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(CREATE)) {
			
			NewContactView createFrame = new NewContactView(that);
			createFrame.setModal(true); //Wait for the JDialog to close
			createFrame.setTitle(CREATE);
			createFrame.setSize(500,600);
			createFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			createFrame.setLocationRelativeTo(null);
			createFrame.setVisible(true);
			
			that=createFrame.getController();
			view.setListModelContacts(that.getListModelAction());
			view.setListModelGroup(that.getGroupsListAction());
			
		}

		if (e.getActionCommand().equals(DETAILS)) {
			
			
			if(view.getSelectedIndexContactList()==-1){ //if nothing is selected
				view.setSelectedIndexContactList(0);
			}
			
			if (view.getSelectedIndexContactList()!=-1){ //the list model is not empty

			
				that.findContactAction(view.getSelectedValueContactList());

				
				DetailsContactView detailDialog = new DetailsContactView(that);
				detailDialog.setModal(true); //Wait for the JDialog to close
				detailDialog.setTitle(DETAILS);
				detailDialog.setSize(600,500);
				detailDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				detailDialog.setLocationRelativeTo(null);
				detailDialog.setVisible(true);
			
			}
			else {
				JOptionPane.showMessageDialog(null,NOTHING_IN_THE_LIST);
			}
		}
		
		if(e.getActionCommand().equals(DELETE)) {
			
			
			if(view.getSelectedIndexContactList()==-1){ //if nothing is selected
				view.setSelectedIndexContactList(0);
			}
			
			if (view.getSelectedIndexContactList()!=-1){ //the list model is not empty
				
				that.findContactAction(view.getSelectedValueContactList());
				that.deleteContactAction(that.getContact());
				view.setListModelContacts(that.getListModelAction());
				view.setListModelGroup(that.getGroupsListAction());
			}
			else {
				JOptionPane.showMessageDialog(null,NOTHING_IN_THE_LIST);
			}
		}
		
		if (e.getActionCommand().equals(MODIFIE)){
			
			if(view.getSelectedIndexContactList()==-1){ //if nothing is selected
				view.setSelectedIndexContactList(0);
			}
			
			if (view.getSelectedIndexContactList()!=-1){ //the list model is not empty
				
				that.findContactAction(view.getSelectedValueContactList());
				
				ModifieContactView modifieFrame = new ModifieContactView(that);
				modifieFrame.setModal(true); //Wait for the JDialog to close
				modifieFrame.setTitle(MODIFIE);
				modifieFrame.setSize(500,600);
				modifieFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				modifieFrame.setLocationRelativeTo(null);
				modifieFrame.setVisible(true);
				
				that=modifieFrame.getController();
				view.setListModelContacts(that.getListModelAction());
				view.setListModelGroup(that.getGroupsListAction());
				
				
			}
			else {
				JOptionPane.showMessageDialog(null,NOTHING_IN_THE_LIST);
			}
			
			
		}
		
		if (e.getActionCommand().equals(SEARCH)) {
			
			view.setListModelContacts(that.getListModelSearchAction(view.getSearch()));
		}
		
		if (e.getActionCommand().equals(SORTBYNAME)) {
			
			that.sortByNameAction();
			view.setListModelContacts(that.getListModelAction());
		}
		
		
		if (e.getActionCommand().equals(SORTBYGROUP)) {
			if(view.getSelectedIndexGroupList()==-1){ //if nothing is selected
				view.setSelectedIndexGroupList(0);
			}
			
			if (view.getSelectedIndexGroupList()!=-1){ //the list model is not empty
			
			view.setListModelContacts(that.getListModelByGroupAction(view.getSelectedValueGroupList()));
			}
			else {
				JOptionPane.showMessageDialog(null,NOTHING_IN_THE_LIST);
			}
			
		}
		
		if (e.getActionCommand().equals(SORTBYDATE)) {
			
			that.sortByDateAction();
			view.setListModelContacts(that.getListModelAction());
		}
		
		if (e.getActionCommand().equals(SAVE)) {
			that.saveAction();
		}
		
		if (e.getActionCommand().equals(LOAD)) {
			that.loadAction();
			view.setListModelContacts(that.getListModelAction());
		}
		
		

	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		int dialogButton = JOptionPane.showConfirmDialog (null,SAVE_QUESTION);
		if (dialogButton == JOptionPane.YES_OPTION){
			that.saveAction();
		}
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		that.loadAction();
		view.setListModelContacts(that.getListModelAction());
		view.setListModelGroup(that.getGroupsListAction());
	}
	
	
	
	

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		if (e.getClickCount() == 1) {
			if (e.getSource().equals(view.getGoupList())){ //if the click comes from the group list
				view.setListModelContacts(that.getListModelByGroupAction(view.getSelectedValueGroupList()));
        	}
		}
		
		
        if (e.getClickCount() == 2) {
        	if (e.getSource().equals(view.getContactList())){ //if the double click comes from the group list
        		
        		that.findContactAction(view.getSelectedValueContactList());
				
				DetailsContactView detailDialog = new DetailsContactView(that);
				detailDialog.setModal(true); //Wait for the JDialog to close
				detailDialog.setTitle(DETAILS);
				detailDialog.setSize(450,300);
				detailDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				detailDialog.setLocationRelativeTo(null);
				detailDialog.setVisible(true);
        		
        
        	}
        	
        }
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		

	}

	@Override
	public void changedUpdate(DocumentEvent arg0) {
		view.setListModelContacts(that.getListModelSearchAction(view.getSearch()));
		
	}

	@Override
	public void insertUpdate(DocumentEvent arg0) {
		view.setListModelContacts(that.getListModelSearchAction(view.getSearch()));
		
	}

	@Override
	public void removeUpdate(DocumentEvent arg0) {
		view.setListModelContacts(that.getListModelSearchAction(view.getSearch()));
		
	}

	

	
}



